var rp = require('request-promise');
const {target} = require("../utils/options");
const fs = require('fs');

const update = async (_id, params) => {
    var options = {
        method: 'PATCH',
        uri: `${target}/movies/${_id}`,
        body: params,
        json: true // Automatically stringifies the body to JSON
    };

    const result = await rp(options);

    if (result._id && params['$pull']) {
        let url =  JSON.parse(params['$pull']);
        if (url.poster) url = url.poster;
        if (url.chaImgs) url = url.chaImgs;
        if (url.images) url = url.images;
        fs.unlink(`./public${url}`, (err) => {
            if (err) throw err;
            console.log(`已成功删除 ./public${url}`);
        });
    }
    return result;
}
const getById = async (_id) => {
    var options = {
        method: 'Get',
        uri: `${target}/movies/${_id}`,
        json: true // Automatically stringifies the body to JSON
    };

    return await rp(options);
}



module.exports = {
    update,
    getById
}